/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DAO.OptionsDAO;
import GUI.Components.IToolbarPerformEvent;
import GUI.Components.JErrorProvider;
import POJO.Options;
import Util.DataAccessUtil;
import Util.MsgUtil;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Nguyễn Thi Mỷ Diện (1140214)
 */
public class FrmOptions extends javax.swing.JInternalFrame {

    private List<Options> listOptions = null;
    private JErrorProvider jErrorControl;
    private org.apache.log4j.Logger log =  org.apache.log4j.Logger.getLogger(this.getClass().getName());
    
    public FrmOptions() {
        initComponents();  
        jErrorControl = new JErrorProvider();
        formLoad();      
    }
    
    private void formLoad(){                
        try {
           listOptions = OptionsDAO.getListOptions();
        } catch (Exception ex) {
            log.error(ex.getMessage(),ex.fillInStackTrace());     
        }
        txtStoreName.setText(listOptions.get(0).getValue());
        txtStoreAddress.setText(listOptions.get(1).getValue());
        txtStorePhone.setText(listOptions.get(2).getValue());
        txtStoreFax.setText(listOptions.get(3).getValue());
        txtStoreEmail.setText(listOptions.get(4).getValue());
        txtStoreWeb.setText(listOptions.get(5).getValue());
        txtStoreArea.setText(listOptions.get(6).getValue());
        txtRepresentator.setText(listOptions.get(7).getValue());        
    }
    
    private List<Options> getOptionsInput(){
        jErrorControl.clear();
        List<Options> list = new ArrayList<>();
        int numError = 0;
        if(txtStoreName.getText().isEmpty()){
            jErrorControl.setError(txtStoreName, "Bạn chưa nhập tên cửa hàng.");
            numError++;
        }
        if(txtStoreAddress.getText().isEmpty()){
             jErrorControl.setError(txtStoreAddress, "Bạn chưa nhập địa chỉ.");
             numError++;
        }
        if(txtStorePhone.getText().isEmpty()){
            jErrorControl.setError(txtStorePhone, "Bạn chưa nhập điện thoại.");
            numError++;
        }
        if(txtStoreFax.getText().isEmpty()){
             jErrorControl.setError(txtStoreFax, "Bạn chưa nhập fax.");
             numError++;
        }
        if(txtStoreEmail.getText().isEmpty()){
            jErrorControl.setError(txtStoreEmail, "Bạn chưa nhập email.");
            numError++;
        }
        if(txtStoreWeb.getText().isEmpty()){
             jErrorControl.setError(txtStoreWeb, "Bạn chưa nhập website.");
             numError++;
        }
        if(txtStoreArea.getText().isEmpty()){
            jErrorControl.setError(txtStoreArea, "Bạn chưa nhập lĩnh vực.");
            numError++;
        }
        if(txtRepresentator.getText().isEmpty()){
             jErrorControl.setError(txtRepresentator, "Bạn chưa nhập người đại diện.");
             numError++;
        }       
        
        list.add(new Options("StoreName", txtStoreName.getText()));
        list.add(new Options("StoreAddress", txtStoreAddress.getText()));
        list.add(new Options("StorePhone", txtStorePhone.getText()));
        list.add(new Options("StoreFax", txtStoreFax.getText()));
        list.add(new Options("StoreEmail", txtStoreEmail.getText()));
        list.add(new Options("StoreWeb", txtStoreWeb.getText()));
        list.add(new Options("StoreArea", txtStoreArea.getText()));
        list.add(new Options("Representator", txtRepresentator.getText()));
        if(numError > 0) return null;          
        return list;
    }
    
   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtRepresentator = new javax.swing.JTextField();
        txtStoreArea = new javax.swing.JTextField();
        txtStoreWeb = new javax.swing.JTextField();
        txtStoreEmail = new javax.swing.JTextField();
        txtStoreFax = new javax.swing.JTextField();
        txtStorePhone = new javax.swing.JTextField();
        txtStoreAddress = new javax.swing.JTextField();
        txtStoreName = new javax.swing.JTextField();
        jPanelLogo = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        btnUpdate = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();

        setTitle("Quy định");
        setMaximumSize(new java.awt.Dimension(600, 450));
        setPreferredSize(new java.awt.Dimension(650, 450));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Thông tin cửa hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jLabel1.setText("Tên cửa hàng:");

        jLabel2.setText("Địa chỉ:");

        jLabel4.setText("Điện thoại:");

        jLabel3.setText("Fax:");

        jLabel8.setText("Email:");

        jLabel7.setText("Website:");

        jLabel5.setText("Lĩnh vực:");

        jLabel6.setText("Người đại diện:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtRepresentator))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5))
                        .addGap(12, 12, 12)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtStoreAddress, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtStorePhone, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtStoreFax, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtStoreEmail, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtStoreWeb, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtStoreArea)
                            .addComponent(txtStoreName, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtStoreName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtStoreAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtStorePhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtStoreFax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtStoreEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtStoreWeb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtStoreArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtRepresentator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelLogo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel9.setText("Logo");

        javax.swing.GroupLayout jPanelLogoLayout = new javax.swing.GroupLayout(jPanelLogo);
        jPanelLogo.setLayout(jPanelLogoLayout);
        jPanelLogoLayout.setHorizontalGroup(
            jPanelLogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLogoLayout.createSequentialGroup()
                .addGap(99, 99, 99)
                .addComponent(jLabel9)
                .addContainerGap(120, Short.MAX_VALUE))
        );
        jPanelLogoLayout.setVerticalGroup(
            jPanelLogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLogoLayout.createSequentialGroup()
                .addGap(98, 98, 98)
                .addComponent(jLabel9)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelLogo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jPanelLogo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnUpdate.setText("Cập nhật");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnClose.setText("Kết thúc");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(99, 99, 99)
                .addComponent(btnUpdate)
                .addGap(18, 18, 18)
                .addComponent(btnClose)
                .addContainerGap(356, Short.MAX_VALUE))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(btnClose))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        List<Options> list;
        list = getOptionsInput();  
        Exception e = new Exception();
        if (list==null) {            
            log.error(e.getMessage(),e.fillInStackTrace());
            MsgUtil.error("Bạn chưa nhập đủ thông tin!",
                                 e.getMessage());      
            
        }
        else{
            boolean update = DataAccessUtil.update(list);
            if (update == true) {
                JOptionPane.showMessageDialog(null, "Cập nhật thành công");
            }
            else{
                log.error(e.getMessage(),e.fillInStackTrace());
                MsgUtil.error("Có lỗi trong quá trình cập nhật!",
                                 e.getMessage()); 
            }
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        if(MsgUtil.yesno("Bạn có muốn lưu thay đổi không?") == JOptionPane.YES_OPTION){
            btnUpdateActionPerformed(evt);
        }
        this.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelLogo;
    private javax.swing.JTextField txtRepresentator;
    private javax.swing.JTextField txtStoreAddress;
    private javax.swing.JTextField txtStoreArea;
    private javax.swing.JTextField txtStoreEmail;
    private javax.swing.JTextField txtStoreFax;
    private javax.swing.JTextField txtStoreName;
    private javax.swing.JTextField txtStorePhone;
    private javax.swing.JTextField txtStoreWeb;
    // End of variables declaration//GEN-END:variables
}
